﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using OpenExpressApp.Editors;
using System.Windows.Data;
using Csla.Wpf;
using System.Windows;
using OpenExpressApp.WPF;

namespace OpenExpressApp.Module.WPF.Editors
{
    /// <summary>
    /// 含有Button的属性编辑器
    /// 
    /// 子类重写OnButtonClick事件即可直接写具体事件代码。
    /// </summary>
    public abstract class ButtonPropertyEditor : WPFPropertyEditor
    {
        private TextBox _tb;
        private DockPanel _panel;

        public ButtonPropertyEditor(BusinessObjectPropertyInfo propertyInfo, IObjectView view)
            : base(propertyInfo, view) { }

        /// <summary>
        /// 创建一个DockPanel
        /// 其中包含了一个Button和一个TextBox
        /// </summary>
        /// <returns></returns>
        protected override FrameworkElement CreateEditingElement()
        {
            this._panel = new DockPanel();

            //create a button
            Button btnPop = new Button() { Content = "..." };
            btnPop.SetValue(DockPanel.DockProperty, Dock.Right);
            btnPop.Click += new RoutedEventHandler(btnPop_Click);
            this._panel.Children.Add(btnPop);

            //create a textbox
            this._tb = new TextBox()
            {
                Name = PropertyInfo.Name,
            };
            //绑定TextBox到对象属性
            Binding tbbinging = new Binding(PropertyInfo.Name);
            this._tb.SetBinding(TextBox.TextProperty, tbbinging);
            this._panel.Children.Add(this._tb);

            // 设定宽度和高度限制
            _panel.Height = EditorConst.MemoEditorHeight;

            return this._panel;
        }
        /// <summary>
        /// 点击按钮后发生的事件。
        /// 子类实现
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnButtonClick(RoutedEventArgs e) { }

        private void btnPop_Click(object sender, RoutedEventArgs e)
        {
            this.OnButtonClick(e);
        }

        protected override FrameworkElement CreateReadOnlyElement()
        {
            this._panel = new DockPanel();

            //create a button
            Button btnPop = new Button() { Content = "..." };
            btnPop.SetValue(DockPanel.DockProperty, Dock.Right);
            btnPop.Click += new RoutedEventHandler(btnPop_Click);
            this._panel.Children.Add(btnPop);

            //create a textbox
            TextBlock tb = new TextBlock()
            {
                Name = PropertyInfo.Name,
            };
            //绑定TextBox到对象属性
            Binding tbbinging = new Binding(PropertyInfo.Name);
            tb.SetBinding(TextBlock.TextProperty, tbbinging);
            this._panel.Children.Add(tb);

            // 设定宽度和高度限制
            _panel.Height = EditorConst.MemoEditorHeight;

            return this._panel;
        }


    }
}